package com.zzj.ecology.admin.controller.system;

import com.zzj.ecology.admin.controller.BaseController;
import com.zzj.ecology.common.constant.UrlConstants;
import com.zzj.ecology.common.enums.BusinessType;
import com.zzj.ecology.framework.annotation.Log;
import com.zzj.ecology.framework.annotation.RepeatSubmit;
import com.zzj.ecology.system.bean.system.SysUrl;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.service.system.SysUrlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * SysUrl <-> SysPermission 是 1:1 关系
 * SysUrl <-  SysPermission 是 N:1 关系
 */
@Api(value = "SysUrl", description = "SysUrl 请求路径相关操作接口")
@RestController
public class SysUrlController extends BaseController {

    @Autowired
    private SysUrlService sysUrlService;

    /**
     * 分页查询 URL 列表数据
     * @param page
     * @return
     */
    @ApiOperation(value = "分页查询所有URL请求地址", notes = "分页查询所有URL请求地址", httpMethod = "GET")
    @RequestMapping(value = UrlConstants.UrlUrl.LIST)
    public HttpResult findPageList(Page page) {
        HttpResult result = sysUrlService.findPageList(page);
        return success(result);
    }

    @Log(title = "URL请求地址管理",businessType = BusinessType.INSERT)
    @ApiOperation(value = "创建URL请求并分配权限", notes = "创建一条新的URL请求地址，并给其分配权限", httpMethod = "POST")
    @RepeatSubmit
    @PostMapping(value = UrlConstants.UrlUrl.CREATE_AND_GRANT)
    public HttpResult createAndGrant(SysUrl sysUrl, @RequestParam("permissionId") Integer permissionId) {
        sysUrl.setCreateAt(new Date());
        sysUrl.setCreateBy(getUsername());
        HttpResult result = sysUrlService.createAndGrant(sysUrl, permissionId);
        return success(result);
    }

    @Log(title = "URL请求地址管理",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改URL请求并重新分配权限", notes = "根据既存的URL请求地址的ID修改内容，并给其重新分配权限", httpMethod = "POST")
    @RepeatSubmit
    @PostMapping(value = UrlConstants.UrlUrl.UPDATE_AND_GRANT)
    public HttpResult updateAndGrant(SysUrl sysUrl, @RequestParam("permissionId") Integer permissionId) {
        sysUrl.setUpdateAt(new Date());
        sysUrl.setUpdateBy(getUsername());
        HttpResult result = sysUrlService.updateAndGrant(sysUrl, permissionId);
        return success(result);
    }

    @ApiOperation(value = "批量删除", notes = "批量删除菜单", httpMethod = "POST")
    @RepeatSubmit
    @Log(title = "URL请求地址管理",businessType = BusinessType.DELETE)
    @PostMapping(value = UrlConstants.UrlUrl.DELETE_BY_IDS)
    public HttpResult updateDeleteStatusByIds(@RequestParam("ids") List<Integer> urlIds) {
        HttpResult result = HttpResult.error();
        if (urlIds.size() > 0) {
            result = sysUrlService.updateDeleteStatusByIds(urlIds, getUsername());
        }
        return success(result);
    }

    @ApiOperation(value = "查询URL请求路径是否存在", notes = "查询URL请求路径是否存在， 存在返回SUCCESS， 不存在返回 ERROR", httpMethod = "POST")
    @PostMapping(value = UrlConstants.UrlUrl.CHECK_EXISTS)
    public String checkExists(String urlPath) {
        //boolean b = sysUrlService.checkUrlPathExists(urlPath);
        return sysUrlService.checkUrlPathExists(urlPath) ? HttpResult.SUCCESS_TAG : HttpResult.FAILURE_TAG;
    }


}
